﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YArchitech.ProgressBar;
using YJKRevitBase.WinForm;
using YJKRevitKernel.RevitDimensionUtility;

namespace YJKRevitAxisModule
{
	public partial class DlgViewSelect : System.Windows.Forms.Form
	{
		public List<Autodesk.Revit.DB.View> ViewList { get; set; }

		public List<Level> LevelList { get; set; }

		public GraphicsStyle Style { get; set; }

		public TextNoteType TextStyle { get; set; }

		public DlgViewSelect(List<Autodesk.Revit.DB.View> viewList, List<GraphicsStyle> styleList, List<TextNoteType> textTypes, List<Level> levels, Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
			this.InitializeComponent();
			this.ViewList = new List<Autodesk.Revit.DB.View>();
			this.LevelList = new List<Level>();
			foreach (Autodesk.Revit.DB.View view in viewList)
			{
				DisplayView item = new DisplayView(view);
				this.clbView.Items.Add(item);
			}
			foreach (GraphicsStyle item2 in styleList)
			{
				this.cbxStyle.Items.Add(item2);
			}
			if (this.cbxStyle.Items.Count > 0)
			{
				this.cbxStyle.SelectedIndex = 0;
			}
			foreach (TextElementType item3 in textTypes)
			{
				this.cbxFront.Items.Add(item3);
			}
			if (this.cbxFront.Items.Count > 0)
			{
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				if (Convert.ToInt32(this.m_doc.Application.VersionNumber) > 2016)
				{
					list.Add("HY_仿宋_3.0mm");
					list.Add("HY_仿宋_3mm");
					list.Add("仿宋_3.0mm");
					list.Add("仿宋_3mm");
					list2.Add("_仿宋_");
					list2.Add("仿宋");
					list2.Add("_3.0mm");
					list2.Add("_3mm");
				}
				else
				{
					list.Add("HY_仿宋_3.5mm");
					list.Add("仿宋_3.5mm");
					list2.Add("_仿宋_");
					list2.Add("仿宋");
					list2.Add("_3.5mm");
				}
				string fortText = RevitDimensionUtiltyMethod.GetFortText(textTypes, list, list2);
				if (fortText != null && fortText != string.Empty)
				{
					this.cbxFront.Text = fortText;
				}
				else
				{
					this.cbxFront.SelectedIndex = 0;
				}
			}
			levels = (from c in levels
			orderby c.Elevation
			select c).ToList<Level>();
			foreach (Level level in levels)
			{
				DisplayLevel item4 = new DisplayLevel(level);
				this.clbPlane.Items.Add(item4);
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.clbView.CheckedItems.Count > 0 && this.clbPlane.CheckedItems.Count > 0)
			{
				this.ViewList.Clear();
				this.LevelList.Clear();
				foreach (object obj in this.clbView.CheckedItems)
				{
					DisplayView displayView = (DisplayView)obj;
					this.ViewList.Add(displayView.View);
				}
				foreach (object obj2 in this.clbPlane.CheckedItems)
				{
					DisplayLevel displayLevel = (DisplayLevel)obj2;
					this.LevelList.Add(displayLevel.Level);
				}
				base.DialogResult = DialogResult.OK;
				this.Style = (this.cbxStyle.SelectedItem as GraphicsStyle);
				this.TextStyle = (this.cbxFront.SelectedItem as TextNoteType);
				return;
			}
			YJKMessageBox.Show("请至少选择一个三维视图和一个生成轴线的层。");
		}

		private void label1_Click(object sender, EventArgs e)
		{
		}

		private void DlgViewSelect_Load(object sender, EventArgs e)
		{
		}

		private void clbView_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.clbView.SelectedItem != null && ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.clbView.ClearSelected();
			}
		}

		private void clbView_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.clbView.SelectedItem != null && ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.clbView.ClearSelected();
			}
		}

		private void clbPlane_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.clbPlane.SelectedItem != null && ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.clbPlane.ClearSelected();
			}
		}

		private void clbPlane_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.clbPlane.SelectedItem != null && ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.clbPlane.ClearSelected();
			}
		}

		private void clbView_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void btnClear_Click(object sender, EventArgs e)
		{
			if (this.clbView.CheckedItems.Count == 0 || this.clbPlane.CheckedItems.Count == 0)
			{
				YJKMessageBox.Show("请选择需要显示轴线的三维视图和楼层");
				return;
			}
			if (YJKMessageBox.Show("是否需要清除所有三维轴网？", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				Transaction transaction = new Transaction(this.m_doc, "clear");
				ProgressBarForm progressBarForm = null;
                WindowHandle owner = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				base.Hide();
				try
				{
					transaction.Start();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_Lines).OfClass(typeof(CurveElement));
					FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_TextNotes).OfClass(typeof(TextNote));
					ICollection<ElementId> collection = new List<ElementId>();
					int num = filteredElementCollector.ToElementIds().Count + filteredElementCollector2.ToElementIds().Count;
					if (num > 0)
					{
						progressBarForm = new ProgressBarForm(num, ProgressBarStyle.Blocks, true);
					}
					if (progressBarForm != null)
					{
						progressBarForm.Show(owner);
					}
					foreach (ElementId elementId in filteredElementCollector.ToElementIds())
					{
						if (DlgViewSelect.ReadData(this.m_doc.GetElement(elementId)) == "Grid")
						{
							collection.Add(elementId);
							if (progressBarForm != null)
							{
								if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
								{
									if (transaction.HasStarted())
									{
										transaction.RollBack();
									}
									return;
								}
								progressBarForm.RefreshProgressBar();
								if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
								{
									if (transaction.HasStarted())
									{
										transaction.RollBack();
									}
									return;
								}
							}
						}
					}
					foreach (ElementId elementId2 in filteredElementCollector2.ToElementIds())
					{
						if (DlgViewSelect.ReadData(this.m_doc.GetElement(elementId2)) == "Grid")
						{
							collection.Add(elementId2);
							if (progressBarForm != null)
							{
								if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
								{
									if (transaction.HasStarted())
									{
										transaction.RollBack();
									}
									return;
								}
								progressBarForm.RefreshProgressBar();
								if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
								{
									if (transaction.HasStarted())
									{
										transaction.RollBack();
									}
									return;
								}
							}
						}
					}
					this.m_doc.Delete(collection);
					if (progressBarForm != null)
					{
						progressBarForm.Dispose();
					}
					YJKMessageBox.Show("清理操作成功!");
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
					YJKMessageBox.Show("清理操作失败！");
				}
				base.Show();
			}
		}

		public static string ReadData(Element elem)
		{
			string result;
			try
			{
				Entity entity = elem.GetEntity(Schema.Lookup(CmdGridToModelCurve.DataGuid));
				if (entity == null)
				{
					result = "";
				}
				else
				{
					result = entity.Get<string>(Schema.Lookup(CmdGridToModelCurve.DataGuid).GetField("HYData"));
				}
			}
			catch
			{
				result = "";
			}
			return result;
		}

		internal Document m_doc;

		private DlgViewSelect instance;
	}
}
